package edu.sdwu.liqing.qasystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import edu.sdwu.liqing.qasystem.entity.QaRoleModule;
import edu.sdwu.liqing.qasystem.mapper.QaRoleModuleMapper;
import edu.sdwu.liqing.qasystem.service.IQaRoleModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@Service
public class QaRoleModuleServiceImpl extends ServiceImpl<QaRoleModuleMapper, QaRoleModule> implements IQaRoleModuleService {

    @Override
    public List<String> getModuleIdListByRoleId(String roleId) {
        if (StringUtils.isBlank(roleId)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        QueryWrapper<QaRoleModule> qw = Wrappers.query();
        qw.eq("role_id", roleId);
        List<QaRoleModule> roleModuleList = super.list(qw);
        if (CollectionUtils.isEmpty(roleModuleList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        return roleModuleList.parallelStream().map(QaRoleModule::getModuleId).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleIdListByModuleId(String moduleId) {
        if (StringUtils.isBlank(moduleId)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        QueryWrapper<QaRoleModule> qw = Wrappers.query();
        qw.eq("module_id", moduleId);
        List<QaRoleModule> roleModules = super.list(qw);
        if (CollectionUtils.isEmpty(roleModules)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        return roleModules.parallelStream().map(QaRoleModule::getRoleId).collect(Collectors.toList());
    }
}
